function [Weights Likelihood Y_diff] = logistic_train(data,label,epsilon,maxiterations)
    [N D] = size(data);
    Weights_init = initializeWeights(D);  %zeros 58*1
    Weights_old = Weights_init;
    Likelihood = [];
    Y = [];
    Y_diff = [];
    %iterations
    for i = 1:maxiterations
        i
        [likelihood t y]= computeLikelihood(data,label,Weights_old, N, D);
        Likelihood = [Likelihood likelihood];
        Y = [Y y];
%         Likelihood = [Likelihood i];      
%         if(~likelihood)
%             break;
%         end
%         if((i>1)&&((abs(Likelihood(i) - Likelihood(i-1))<epsilon)))
        if(i>1)
            y_difference = norm(Y(:,i) - Y(:,i-1),2);
            Y_diff = [Y_diff y_difference];
    %         if((i>1)&&(norm(Y(:,i) - Y(:,i-1),2)<epsilon))
            if(y_difference < epsilon)
                break;  
            end
        end
        [H R] = computeHessianMat(data,label,Weights_old,N,D);
        if(0 == det(H))
            break;
        end
%         Weights_new = Weights_old - H^(-1)*data'*(y - t);   
        Weights_new = H^(-1)*data'*(R*data*Weights_old -(y - t));
        Weights_old = Weights_new;
    end  
    Weights = Weights_new;
end